//
//  MqFBLocalShopTableViewController.m
//  Meiqi
//
//  Created by TwistedFate on 16/5/26.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqFBLocalShopTableViewController.h"
#import "MqSingleton.h"
#import "MqBrandDetailShopsModel.h"
#import "MqBrandShopTableViewCell.h"
#import "MqFindBikeShopDetailViewController.h"
@interface MqFBLocalShopTableViewController ()
@property (nonatomic, strong) NSMutableArray *shopsArray;
@property (nonatomic ,strong)UIWebView *webView;

@end

@implementation MqFBLocalShopTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.tableView.rowHeight = 110;
    
    [self dataProcess];
    
    [self.tableView registerClass:[MqBrandShopTableViewCell class] forCellReuseIdentifier:@"MqBrandShopTableViewCell"];
    
}

- (void)dataProcess{
    
   // http://p.biketo.com/api/app/sellers?lat=31.925801&brand_id=6&num=10&start=0&lon=118.895334
    
    NSString *urlString = [NSString stringWithFormat:@"http://p.biketo.com/api/app/sellers?lat=%f&brand_id=%@&num=10&start=0&lon=%f", [MqSingleton shareData].lat, self.brand_id, [MqSingleton shareData].lon];
    
    //  网络请求
    [PKRequestManager requestWithUrlString:urlString parDic:nil Method:GET finish:^(NSData *data) {
        
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
        
        //接受shops数据的数组
        _shopsArray = [NSMutableArray array];
        
        NSArray *arr = dic[@"data"][@"data"];
        for (NSDictionary *shopsDic in arr) {
            MqBrandDetailShopsModel *model = [[MqBrandDetailShopsModel alloc] init];
            [model setValuesForKeysWithDictionary:shopsDic];
            [_shopsArray addObject:model];
        }
        
        
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        
        
        
    } path:nil];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _shopsArray.count;
}

//  返回cell的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    MqBrandShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MqBrandShopTableViewCell"];
    if (cell == nil) {
        cell = [[MqBrandShopTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"Cell"];
    }
    
    MqBrandDetailShopsModel *model = _shopsArray[indexPath.row];
    cell.model  = model;
    
    //_contactButton的点击事件
    [cell.contactButton addTarget:self action:@selector(photoNumber:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //给button添加tag值
    cell.contactButton.tag = 1000 + indexPath.row;
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MqFindBikeShopDetailViewController *shopDetailVC = [[MqFindBikeShopDetailViewController alloc] init];
    MqBrandDetailShopsModel *model = _shopsArray[indexPath.row];
    shopDetailVC.shop_id = model.shop_id;
    
    [self.navigationController pushViewController:shopDetailVC animated:YES];
    
    
}


//拨打电话按钮
- (void)photoNumber:(UIButton *)button
{
    //  法三：借助webView打电话 有弹窗提示并且可以回到原来的应用程序  建议使用
    //  懒加载  因为不需要让webView显示在视图上 我创建它只是为了使用它的一个方法 如果把他添加到self.view上 会遮挡原来的视图
    MqBrandDetailShopsModel *model = _shopsArray[button.tag - 1000];
    NSString *urlString = [NSString stringWithFormat:@"tel://%@",model.contact];
    
    if (_webView == nil) {
        _webView = [[UIWebView alloc]init];
        
        NSURL *url = [NSURL URLWithString:urlString];
        //  创建一个请求
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
        //  通过请求加载webView
        [_webView loadRequest:request];
        
    } else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"该设备不支持电话功能" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
    }
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
